package com.lazar.module1.subject2

import scala.io.StdIn

object Game {
  // 用户
  var user: User = new User
  // 电脑
  var computer: Computer = new Computer
  // 对战次数
  var times = 0

  def init(): Unit ={
    println("--------------欢迎进入游戏世界---------------------")
    println("*************************************************")
    println("*******************猜拳，开始*********************")
    println("*************************************************")
    println()
    println()
    println("请选择对战角色：(1.刘备  2.关羽  3.张飞)")

    val role = StdIn.readInt()
    if (role > 3 || role < 1){
      println("输入不规范，结束游戏！！")
      return
    }
    //创建人机
    computer.InitRole(role)
    println(s"你选择了与${computer.name}对战")

    println("要开始么？y/n")
    val yn = StdIn.readLine()
    if (yn.equals("y")){
       start()
    }
    println(s"总共对战$times")
    println(s"电脑得分：${computer.score}")
    println(s"ni得分：${user.score}")
    println("游戏结束")
  }

  def start(): Unit ={
    while (true) {
      times = times + 1
      println("你出拳！1.剪刀 2.石头 3.布")
      var you = StdIn.readInt()
      you = user.showFist(you)
      val com =  computer.showFist()
      val result = compare(you,com)
      if (result == 1){
         user.score=user.score + 1
         println("结果：你赢了")
      }else if (result == -1){
        computer.score=computer.score +1
        println(s"结果：${computer.name}赢了")
      }else{
        println("结果：和局！下次努力！")
      }
      println("是否开始下一轮(y/n)")
      val next = StdIn.readLine()
      if (next.equals("n")){
         return
      }
    }
  }

  def compare(a:Int,b:Int): Int ={
    if (a > b) {
       if ((a - b) == 1){
          1
       }else{
          -1
       }
    }else if (a < b){
      if ((b - a) == 1){
        -1
      }else{
        1
      }
    }else{
      0
    }
  }
  def main(args: Array[String]): Unit = {
      init()
  }
}
